
import java.io.*;
import java.net.*;
import java.util.*;

public class TCPClient extends Thread {
	
	public String caleFisier, idFisier, numeFiltru;
	public int operatie, lungime, inaltime; // 1 - put, 2 - get, 3 - filter, 4 - resize
	
	private String serverIP;
	private int serverPort;
	
	public TCPClient(String serverIP, int serverPort) {
		this.serverIP = serverIP;
		this.serverPort = serverPort;
	}
	
	public static String readFileAsString(String filePath) throws java.io.IOException{
		        StringBuffer fileData = new StringBuffer(1000);
		        BufferedReader reader = new BufferedReader(
		                new FileReader(filePath));
		        char[] buf = new char[1024];
		        int numRead=0;
		        while((numRead=reader.read(buf)) != -1){
		            String readData = String.valueOf(buf, 0, numRead);
		            fileData.append(readData);
		            buf = new char[1024];
		        }
		        reader.close();
		        return fileData.toString();
		    }
	
	public void run() {
		try {
			
			//create file object
		    System.out.println("Calea fisierului pentru " + this.caleFisier);
			String file = "";
			
		    System.out.println("transmit packet info");
						
			Socket clientSocket = new Socket(this.serverIP, this.serverPort);
			PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
			BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			
			switch(this.operatie){
				case 1: 
					file = TCPClient.readFileAsString(this.caleFisier);
					out.println("put " + this.caleFisier + " Start:" +  file.length());
					out.println(file);
				break;
				case 2:
					
					out.println("get " + this.idFisier + " Start:" +  file.length());
				break;
				case 3:
					
					out.println("filter " + this.numeFiltru + " " + this.idFisier + " Start:" +  file.length());
				break;
				case 4:
					out.println("resize " + this.lungime + " " + this.inaltime + " " + this.idFisier + " Start:" +  file.length());
				break;
				default:
					out.println("comanda_necunoscuta" + " Start:" +  file.length());
				break;
			}
			
			
			// Expect the same number of lines back as many were sent.
			String line = in.readLine();
			switch(this.operatie){
			case 1: 
				System.out.println("ID = " + line);
			break;
			case 2:
				System.out.println("Dimensiune fisier:");
				int dimensiune = Integer.parseInt(line);
				char [] charvect = new char [dimensiune];
				byte [] bytevect = new byte [dimensiune];
				int nr_dimensiune = in.read(charvect);
				for(int i=0;i<nr_dimensiune;i++)
					bytevect[i]=(byte)charvect[i];
				FileOutputStream outfisier = new FileOutputStream(this.caleFisier);  
				try {  
				    outfisier.write(bytevect);  
				} finally {  
				    outfisier.close();  
				}
			break;
			case 3:
				System.out.println("ID = " + line);
			break;
			case 4:
				System.out.println("ID = " + line);
			break;
			default:
				System.out.println("comanda_necunoscuta " + line);
			break;		
		}
			System.out.println(line);
			
		} catch (Exception e) {
			System.err.println("TCPClient-" + this.hashCode() + " exception: " + e);
			e.printStackTrace();
		}
	}

}
